function request(opt) {
	return new Promise(async (resolve, reject) => {
		opt = opt || {};
		if (!opt.url) {
			reject('请求地址错误')
			return false
		}
		opt.debug = opt.debug || ''
		opt.data = opt.data || {}
		opt.method = opt.method || 'POST'
		opt.header = {
			"content-type": "application/json",
			...opt.header || {}
		}
		const token = uni.getStorageSync('token') || ''
		if (token) opt.header.Authorization = token
		if (opt.debug) console.log('请求参数：', opt)
		if (!opt.hiddenLoading) uni.showLoading({
			title: opt.method.toLocaleUpperCase() ==  'GET' ? '数据获取中...' : '数据提交中...',
			mask:true
		})
		
		uni.request({
			url: import.meta.env.VITE_BASE_URL + opt.url,
			data: opt.data,
			method: opt.method,
			header: opt.header,
			dataType: 'json',
			success: res => {
				if (!opt.hiddenLoading) uni.hideLoading()
				if (opt.debug) console.log(res)
				if (res.statusCode != 200) {
					console.log(res);
					reject()
					return
				}
				if (res.data.code === 200) {
					resolve(res.data.data)
				} else if (res.data.code == 401) {
					uni.clearStorageSync()
					uni.navigateTo({
						url: '/pages/my/login/login'
					})
				} else {
					uni.showToast({
						icon: 'none',
						title: res.data.msg
					})
					reject(res.data)
				}
			},
			fail: err => {
				if (!opt.hiddenLoading) uni.hideLoading()
				if (opt.debug) console.log(err)
				reject(err)
			}
		})
	})
}

export default request